﻿Namespace PeriodProcessing


    Public Class StudentSatisfiedDiplomaReqs
        Implements IStudentSatisfiedDiplomaReqs

        Private _diplomaSatReqmntProvider As IDiplomaSatisfiedRequirementProvider

        ''' <summary>
        ''' The student we are working on...
        ''' </summary>
        Protected _student As Student

        ''' <summary>
        ''' The Diploma type we are examining
        ''' </summary>
        Protected _diplomaType As DiplomaType

        ''' <summary>
        ''' List of requirements satisfied by this student and this Diploma type
        ''' </summary>
        Protected Property studentSatisfiedRequirements As Dictionary(Of Integer, DiplomaSatisfiedRequirement)

        Public Sub New(ByVal student As Student, ByVal diplomaType As DiplomaType)
            Me._student = student
            Me._diplomaType = diplomaType
        End Sub


        Public Sub loadPastSatisifiedRequirements(ByVal DiplomaRequirementId As Integer)

            If Me.studentSatisfiedRequirements Is Nothing Then
                'load Diplomas satisfied by the student for the Diploma we are
                'checking in a previous period/class

                Dim lst As List(Of DiplomaSatisfiedRequirement) = _
                    Me.DiplomaSatisfiedRequirementProvider.getStudentDiplomaSatisfiedRequirements(Me.diplomaType, Me.Student)

                Me.studentSatisfiedRequirements = New Dictionary(Of Integer,  _
                                                                 DiplomaSatisfiedRequirement)

                For Each csr As DiplomaSatisfiedRequirement In lst
                    Me.studentSatisfiedRequirements.Add(csr.DiplomaRequirementId.GetValueOrDefault, csr)
                Next

            End If
        End Sub

        Public Sub addSatifiedDiplomaRequirement(ByVal certReq As IICModel.BusObjects.DiplomaSatisfiedRequirement) _
                Implements IStudentSatisfiedDiplomaReqs.addSatifiedDiplomaRequirement

            Me.loadPastSatisifiedRequirements(certReq.DiplomaRequirementId.GetValueOrDefault)

            'add to the list of satisfied requirements using the DiplomaRequirementId as the key of the dictionary collection
            If Me.studentSatisfiedRequirements.ContainsKey(certReq.DiplomaRequirementId.GetValueOrDefault) Then
                'Debug.WriteLine("Student: " & Me.Student.StudentId & " studentSatisfiedRequirement already exists: " & certReq.DiplomaRequirementId.GetValueOrDefault)
            Else
                'Debug.WriteLine("Student: " & Me.Student.StudentId & " Add new studentSatisfiedRequirement: " & certReq.DiplomaRequirementId.GetValueOrDefault)
                Me.studentSatisfiedRequirements.Add(certReq.DiplomaRequirementId.GetValueOrDefault, certReq)
            End If

        End Sub

        Public ReadOnly Property diplomaType As IICModel.BusObjects.DiplomaType Implements IStudentSatisfiedDiplomaReqs.diplomaType
            Get
                Return _diplomaType
            End Get
        End Property

        Public Function isDiplomaEarned() As Boolean Implements IStudentSatisfiedDiplomaReqs.isDiplomaEarned

            If Me.Student.MemberNumber = PeriodProcessor.TRACK_STUDENT_NUMBER Then
                Trace.TraceInformation("Checking Diploma for Member Number: {0} : {1}", _
                             Me.Student.MemberNumber, Me.diplomaType.DiplomaType)
            End If

            'loop thru each Diploma requirement of the certificate I am looking to see if the student passed it
            For Each cr As DiplomaRequirement In Me.diplomaType.DiplomaRequirements

                If Me.Student.MemberNumber = PeriodProcessor.TRACK_STUDENT_NUMBER Then
                    Trace.TraceInformation("Checking Diploma requirement for Member Number: {0} : {1}", _
                                 Me.Student.MemberNumber, cr.Certification.CertificationType)
                End If

                If Me.studentSatisfiedRequirements.ContainsKey(cr.DiplomaRequirementId) = False Then
                    'if DiplomaRequirementId is not included in the collection of satisifed requirements,
                    'then we just exit and return false

                    If Me.Student.MemberNumber = PeriodProcessor.TRACK_STUDENT_NUMBER Then
                        Trace.TraceInformation("Member Number: {0} Diploma NOT PASSED: {1}, missing {2}", _
                                         Me.Student.MemberNumber, Me.diplomaType.DiplomaType, _
                                         cr.Certification.CertificationType)
                    End If
                    Return False
                End If

            Next
            If Me.Student.MemberNumber = PeriodProcessor.TRACK_STUDENT_NUMBER Then
                Trace.TraceInformation("Member Number: {0} passed Diploma: {1}", _
                                 Me.Student.MemberNumber, Me.diplomaType.DiplomaType)
            End If

            'if we make it here the it means that all DiplomaRequirement were satisfied
            Return True

        End Function

        Public ReadOnly Property Student As IICModel.BusObjects.Student Implements IStudentSatisfiedDiplomaReqs.Student
            Get
                Return _student
            End Get
        End Property

        Public ReadOnly Property SatisfiedRequirements As List(Of DiplomaSatisfiedRequirement) _
                        Implements IStudentSatisfiedDiplomaReqs.SatisfiedRequirements

            Get
                Return Me.studentSatisfiedRequirements.Values.ToList
            End Get
        End Property

        Public Property DiplomaSatisfiedRequirementProvider() _
                   As IDiplomaSatisfiedRequirementProvider
            Get
                If _diplomaSatReqmntProvider Is Nothing Then
                    _diplomaSatReqmntProvider = New DBDiplomaSatisfiedRequirementProvider
                End If

                Return _diplomaSatReqmntProvider
            End Get
            Set(ByVal value As IDiplomaSatisfiedRequirementProvider)
                _diplomaSatReqmntProvider = value
            End Set
        End Property

    End Class
End Namespace
